using System;

namespace OrbitOne.Elmah.Logic
{
    /// <summary>
    /// Represents a OrbitOneElmahErrorFacade.
    /// </summary>
    public class OrbitOneElmahErrorFacade
    {
        #region Enums
        /// <summary>
        /// The possible user type values
        /// </summary>
        public enum UserTypeEnum
        {
            /// <summary>An internal user / home user</summary>
            InternalUser = 0,
            /// <summary>An anonymous user</summary>
            Anonymous,
            /// <summary>An authenticated user</summary>
            Known,
            /// <summary>A bot (spider, webcrawler)</summary>
            Bot
        }
        #endregion

        #region Properties
        /// <summary>
        /// Gets the error's id.
        /// </summary>
        public Guid Id { get; private set; }
        /// <summary>
        /// Gets the error's happening time
        /// </summary>
        public DateTime HappeningTime { get; private set; }
        /// <summary>
        /// Gets the error's hostname
        /// </summary>
        public string HostName { get; private set; }
        /// <summary>
        /// Gets the application name
        /// </summary>
        public string ApplicationName { get; private set; }
        /// <summary>
        /// Gets the error message
        /// </summary>
        public string ErrorMessage { get; private set; }
        /// <summary>
        /// Gets the ErrorType
        /// </summary>
        public string ErrorType { get; private set; }
        /// <summary>
        /// Gets the ErrorLevel
        /// </summary>
        public int ErrorLevel { get; private set; }
        /// <summary>
        /// Gets the User type 
        /// </summary>
        public UserTypeEnum UserType { get; private set; }
        /// <summary>
        /// Gets the UserInfo
        /// </summary>
        public string UserInfo { get; private set; }
        #endregion

        #region Constructor
        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <param name="happeningTime"></param>
        /// <param name="hostName"></param>
        /// <param name="applicationName"></param>
        /// <param name="errorMessage"></param>
        /// <param name="errorType"></param>
        /// <param name="errorLevel"></param>
        /// <param name="userName"></param>
        /// <param name="appliedRules"></param>
        public OrbitOneElmahErrorFacade(Guid id,
                                        DateTime happeningTime, 
                                        string hostName, 
                                        string applicationName, 
                                        string errorMessage, 
                                        string errorType, 
                                        int errorLevel,
                                        string userName, 
                                        string appliedRules)
        {
            this.Id = id;
            this.HappeningTime = happeningTime;
            this.HostName = hostName;
            this.ErrorType = errorType;
            this.ApplicationName = applicationName;
            this.ErrorMessage = errorMessage;
            this.ErrorLevel = errorLevel;
            GetUserInfo(userName,appliedRules);
        }

        /*/// <summary>
        /// Creates a OrbitOneElmahErrorFacade
        /// </summary>
        /// <param name="error">The <code>OrbitOneElmahErrorEntity</code> that is the base of the OrbitOneElmahErrorFacade</param>
        public OrbitOneElmahErrorFacade(OrbitOneElmahErrorEntity error)
        {
            if (error == null) throw new ArgumentNullException("error");

            Id = error.Id;
            HappeningTime = error.HappeningTime;
            HostName = error.HostName;
            ApplicationName = error.ApplicationName;
            ErrorMessage = error.ErrorMessage;
            ErrorType = error.ErrorType;
            ErrorLevel = error.ErrorLevel;
            GetUserInfo(error.UserName, error.AppliedRules);
        }*/
        #endregion

        #region Private Methods
        private void GetUserInfo(string user, string rules)
        {
            if (string.IsNullOrEmpty(user))
            {
                UserInfo = "Anonymous user";
                UserType = UserTypeEnum.Anonymous;
            }
            else
            {
                UserInfo = user;
                UserType = UserTypeEnum.Known;
            }
            if (!string.IsNullOrEmpty(rules))
            {
                if (rules.Contains(Constants.RuleNameBots))
                {
                    UserInfo = Constants.RuleNameBots;
                    UserType = UserTypeEnum.Bot;
                }
                if (rules.Contains(Constants.RuleInternalUser))
                {
                    UserInfo = Constants.RuleInternalUser;
                    UserType = UserTypeEnum.InternalUser;
                }
            }
        }
        #endregion
    }
}